import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class SingleLineInputWidget extends StatelessWidget {
  final double width;
  final double height;
  final Color background;
  final BorderRadius? borderRadius;
  final BoxBorder? border;
  final EdgeInsets? margin;
  final EdgeInsets? padding;
  final TextStyle textStyle;
  final String hitText;
  final TextStyle hitTextStyle;
  final int inputMaxLength;
  final RegExp? inputFilterPattern;
  final Widget prefixIcon;
  final ValueChanged<String>? onChange;
  final ValueChanged<String>? onSubmit;
  final TextInputAction? action;

  const SingleLineInputWidget(
      {Key? key,
      required this.width,
      required this.height,
      this.background = Colors.white,
      this.borderRadius,
      this.border,
      this.margin,
      this.padding,
      this.inputMaxLength = 50,
      this.inputFilterPattern,
      required this.textStyle,
      required this.hitText,
      required this.hitTextStyle,
      required this.prefixIcon,
      this.onChange,
      this.onSubmit,
      this.action})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width,
      height: height,
      margin: margin,
      decoration: BoxDecoration(border: border, color: background, borderRadius: borderRadius),
      child: Theme(
          data: Theme.of(context).copyWith(
            colorScheme: ThemeData().colorScheme.copyWith(
                  primary: hitTextStyle.color,
                ),
          ),
          child: TextField(
            onChanged: onChange,
            onSubmitted: onSubmit,
            maxLines: 1,
            keyboardType: TextInputType.text,
            inputFormatters: [
              LengthLimitingTextInputFormatter(inputMaxLength),
              FilteringTextInputFormatter(inputFilterPattern ?? RegExp("[a-zA-Z]|[0-9]|[\u4e00-\u9fa5]"), allow: true)
            ],
            textInputAction: action,
            style: textStyle,
            decoration: InputDecoration(
              hintText: hitText,
              hintStyle: hitTextStyle,
              contentPadding: const EdgeInsets.only(top: 0, bottom: 0),
              prefixIcon: prefixIcon,
              prefixIconConstraints: const BoxConstraints(),
              prefixIconColor: hitTextStyle.color,
              border: const OutlineInputBorder(
                borderSide: BorderSide(
                  color: Colors.transparent,
                ),
              ),
              enabledBorder: const OutlineInputBorder(
                borderSide: BorderSide(
                  color: Colors.transparent,
                ),
              ),
              disabledBorder: const OutlineInputBorder(
                borderSide: BorderSide(
                  color: Colors.transparent,
                ),
              ),
              focusedBorder: const OutlineInputBorder(
                borderSide: BorderSide(
                  color: Colors.transparent,
                ),
              ),
            ),
          )),
    );
  }
}
